package com.example.seakdreamandroid.firstPage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.MusicStoryPlayerActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.AudioInfo;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.playHistory.HistoryRecord;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RecommendAudioAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<RecommendAudio> recommendAudioList;
    private static final int LAYOUT_ID = R.layout.item_recommend;

    public RecommendAudioAdapter(Context context, List<RecommendAudio> recommendAudioList) {
        this.context = context;
        this.recommendAudioList = recommendAudioList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View itemView = layoutInflater.inflate(LAYOUT_ID,parent,false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        Holder holder = (Holder)viewHolder;
        RecommendAudio recommendAudio = recommendAudioList.get(position);
        holder.recTit.setText(recommendAudio.getTitle());
        System.out.println(recommendAudio.getImageUrl());
        Glide.with(context).load(recommendAudio.getImageUrl()).into(holder.recIma);
        holder.duration.setText(recommendAudio.getTime());
    }

    @Override
    public int getItemCount() {
        return recommendAudioList.size();
    }

    public void setData(List<RecommendAudio> recommends) {
        this.recommendAudioList = recommends;
    }

    private class Holder extends RecyclerView.ViewHolder {
        public ImageView recIma;
        public TextView recTit;
        public TextView duration;
        public Holder(@NonNull View itemView) {
            super(itemView);
            recIma = itemView.findViewById(R.id.rec_ima);
            recTit = itemView.findViewById(R.id.rec_tit);
            duration = itemView.findViewById(R.id.duration);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    RecommendAudio recommendAudio = recommendAudioList.get(position);
                    User user = (User) RecommendAudioAdapter.this.context.getApplicationContext();
                    Log.i("RecommendAudioAdapter","isWhite="+recommendAudio.isWhite());
                    if (recommendAudio.isWhite()){
                        Log.i("RecommendAudioAdapter","isWhite");
                        List<AudioInfo> audioInfos  = new ArrayList<>();
                        if (recommendAudio.getWhiteCount()  >= 1){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(recommendAudio.getWhiteId1());
                            audioInfo.setAudioName(recommendAudio.getWhiteName1());
                            audioInfo.setAudioPic(recommendAudio.getWhiteImg1());
                            audioInfo.setAudioUrl(recommendAudio.getWhiteUrl1());
                            audioInfos.add(audioInfo);
                        }
                        if (recommendAudio.getWhiteCount()  >= 2){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(recommendAudio.getWhiteId2());
                            audioInfo.setAudioName(recommendAudio.getWhiteName2());
                            audioInfo.setAudioPic(recommendAudio.getWhiteImg2());
                            audioInfo.setAudioUrl(recommendAudio.getWhiteUrl2());
                            audioInfos.add(audioInfo);
                        }
                        if (recommendAudio.getWhiteCount()  >= 3){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(recommendAudio.getWhiteId3());
                            audioInfo.setAudioName(recommendAudio.getWhiteName3());
                            audioInfo.setAudioPic(recommendAudio.getWhiteImg3());
                            audioInfo.setAudioUrl(recommendAudio.getWhiteUrl3());
                            audioInfos.add(audioInfo);
                        }
                        WhiteNoisePlayerService.getController().play(audioInfos,"OfficialRecommendation",null,true);
                        Intent intent = new Intent(context, WhiteNoisePlayerActivity.class);
                        context.startActivity(intent);
                    }else{
                        Intent intent = new Intent(context, MusicStoryPlayerActivity.class);
                        intent.putExtra("audioUrl",recommendAudio.getAudioUrl());
                        intent.putExtra("audioId",recommendAudio.getAudioId());
                        context.startActivity(intent);
                    }
                }
            });
        }
    }

    public void setRecommendAudioList(List<RecommendAudio> recommendAudioList) {
        this.recommendAudioList = recommendAudioList;
        notifyDataSetChanged();
    }

    public List<RecommendAudio> getRecommendAudioList() {
        return recommendAudioList;
    }
}
